package com.su.ch;

import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.su.ch.utils.XmppTool;

public class LoginActivity extends Activity {
	private final int LOGIN_SUCCESS = 1;
	private final int LOGIN_FAILED = 2;
	
	private EditText accountEditText;
	private EditText passwordEditText;
	private CheckBox remeberCheckBox;
	private Button cancel_btn;
	private Button login_btn;
	
	private ProgressDialog pd;//登陆提示框

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_ly);

		// 开启严格模式
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
		StrictMode.setThreadPolicy(policy);

		initView();
	}

	/**
	 * 初始化视图控件
	 */
	public void initView() {
		accountEditText = (EditText) findViewById(R.id.login_account_ET);
		passwordEditText = (EditText) findViewById(R.id.login_password_ET);
		remeberCheckBox = (CheckBox) findViewById(R.id.login_remember_CB);

		// 记住保存的状态
		SharedPreferences preferences = getSharedPreferences("ch.config",
				MODE_PRIVATE);
		boolean ischeck = preferences.getBoolean("remeber", false);
		remeberCheckBox.setChecked(ischeck);
		
		//只有在设置保存密码后才填充数据
		if(ischeck){
			String account = preferences.getString("account", null);
			String password = preferences.getString("password", null);
			accountEditText.setText(account);
			passwordEditText.setText(password);
		}

		cancel_btn = (Button) findViewById(R.id.login_cancel_btn);
		cancel_btn.setOnClickListener(listener);
		login_btn = (Button) findViewById(R.id.login_ok_btn);
		login_btn.setOnClickListener(listener);

	}
	
	//这个在这里主要用来处理联网的，联网完成取消对话框显示
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case LOGIN_SUCCESS:
				Toast.makeText(LoginActivity.this, "登陆成功", 0).show();
				pd.dismiss();
				Intent intent = new Intent(LoginActivity.this, ChatActivity.class);
				startActivity(intent);
				finish();
				break;
			case LOGIN_FAILED:
				Toast.makeText(LoginActivity.this, "登陆失败，请检查您的密码是否正确", 0).show();
				pd.dismiss();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
		
	};

	private OnClickListener listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.login_cancel_btn:
				finish();
				break;
			case R.id.login_ok_btn:
				login();
				break;
			default:
				break;
			}
		}
	};


	public void login() {
		final String account = accountEditText.getText().toString();
		final String password = passwordEditText.getText().toString();
		//如果选中要记住账号
		if (remeberCheckBox.isChecked()) {
			SharedPreferences preferences = getSharedPreferences(
					"ch.config", MODE_PRIVATE);
			Editor editor = preferences.edit();
			editor.putString("account", account);
			editor.putString("password", account);
			editor.putBoolean("rember", true);
			editor.commit();
		}

		pd = new ProgressDialog(LoginActivity.this);
		pd.setMessage("在登陆，莫捉急");
		pd.setCancelable(false);
		pd.show();

		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					XmppTool.getConnection().login(account, password);
					Presence presence = new Presence(Presence.Type.available);// 在线
					XmppTool.getConnection().sendPacket(presence);
					
					ChatActivity.account = account;
					
					Message msg = new Message();
					msg.what = LOGIN_SUCCESS;
					handler.sendMessage(msg);
				} catch (XMPPException e) {
					e.printStackTrace();
					Message msg = new Message();
					msg.what = LOGIN_FAILED;
					handler.sendMessage(msg);
				}
				
			}
		}).start();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		return true;
	}

}
